<?php

namespace app\common\model\product;

use app\common\model\BaseModel;
use app\common\model\order\Order;
use app\common\model\store\Clerk;
use app\common\model\store\Store;
use app\common\model\supplier\Supplier;


/**
 * 商品模型
 */
class ProductClaim extends BaseModel
{
    protected $name = 'product_claim';
    protected $pk = 'id';

    public function order()
    {
        return $this->hasOne(Order::class, 'order_id', 'order_id');
    }

    public function supplier()
    {
        return $this->hasOne(Supplier::class, 'shop_supplier_id', 'shop_supplier_id');
    }

    public function store()
    {
        return $this->hasOne(Store::class, 'store_id', 'store_id');
    }

    public function storeClerk()
    {
        return $this->hasOne(Clerk::class, 'clerk_id', 'store_clerk_id');
    }

    public static function listData($where, $params, $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($post_data = [])
    {
        if($post_data){
            $model = new static();
            $post_data['app_id'] = self::$app_id;
            $data = $model->save($post_data);
            return $data;
        }

    }

    public static function one($where, $with = [])
    {
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }
}
